package com.yege.table;

import com.yege.uilt.ByteUtil;
import lombok.Data;
import org.apache.hadoop.hbase.client.HTable;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

@Data
public class HTableRow {

    private byte[] rowkey;
    private Map<String,byte[]> map;

    public HTableRow(byte[] rowkey, Map<String, byte[]> map) {
        this.rowkey = rowkey;
        this.map = map;
    }

    public static HTableRow build(HTableBase hTableBase){
        byte[] rowkey = hTableBase.rowkey();
        Map<String,byte[]> map = new HashMap<>();
        try {
            Field[] arr = hTableBase.getClass().getDeclaredFields();
            for (Field field : arr) {
//                System.out.println(field.getName());
                field.setAccessible(true);
                Object o = field.get(hTableBase);
//                System.out.println(o);
                map.put(field.getName(), ByteUtil.toByte(o));
            }
        }catch (Exception e) {
            e.printStackTrace();
        }

        return new HTableRow(rowkey,map);
    }

}
